﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Steamrunner
{
    class IniFile
    {
        #region Members
        private readonly string filename;

        private Dictionary<string, Dictionary<string, float>>
            options = new Dictionary<string, Dictionary<string, float>>();
        #endregion

        public Dictionary<string, Dictionary<string, float>> Options
        {
            get { return options; }
            set { options = value; }
        }

        public string Filename
        {
            get { return filename; }
            //set { filename = value; }
        }

        public IniFile(string filename)
        {
            this.filename = filename;
        }

        public float GetValue(string group, string key)
        {
            return options[group][key];
        }

        public void AddModify(string group, string key, float value)
        {
            if (!options.ContainsKey(group))
            {
                options.Add(group, new Dictionary<string, float>());
            }

            if (!options[group].ContainsKey(key))
            {
                options[group].Add(key, value);
            }
            else
            {
                options[group][key] = value;
            }
        }

        public void Parse()
        {
            TextReader iniFile = null;

            if (System.IO.File.Exists(filename))
            {
                string line;
                string currentGroup = null;
                string[] keyPair;
                iniFile = new StreamReader(filename);

                line = iniFile.ReadLine();
                while (line != null)
                {
                    if (line != "")
                    {
                        if (line.StartsWith("[") && line.EndsWith("]"))
                        {
                            currentGroup = line.Substring(1, line.Length - 2);
                            options.Add(currentGroup, new Dictionary<string, float>());
                        }
                        else
                        {
                            keyPair = line.Split(new char[] { '=' }, 2);
                            options[currentGroup].Add( keyPair[0], System.Convert.ToSingle(keyPair[1].Replace(".", ",")) );
                        }
                    }
                    line = iniFile.ReadLine();
                }
                iniFile.Close();
            }
        }

        public void Save()
        {
            TextWriter iniFile = new StreamWriter(filename);

            foreach (KeyValuePair<string, Dictionary<string, float>> group in options)
            {
                iniFile.WriteLine("[" + group.Key + "]");

                foreach (KeyValuePair<string, float> key in group.Value)
                {
                    iniFile.WriteLine(key.Key + "=" + key.Value);
                }
            }
            iniFile.Close();
        }
    }
}
